package com.ftg.midamall.admin.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ftg.midamall.admin.dto.AdminInfo;
import com.ftg.midamall.admin.dto.ModifyAdminInfo;
import com.ftg.midamall.admin.dto.UserInfo;
import com.ftg.midamall.admin.entity.UmsAdmin;
import com.ftg.midamall.admin.entity.UmsAdminRoleRelation;
import com.ftg.midamall.admin.service.IUmsAdminService;
import com.ftg.midamall.admin.service.impl.UmsAdminServiceImpl;
import com.ftg.midamall.admin.vo.UmsAdminVo;
import com.ftg.midamall.comm.vo.CodeStatusEnum;
import com.ftg.midamall.comm.vo.RespMessUtil;
import com.ftg.midamall.config.ConstClass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author qjw
 * @since 2023-10-20
 */
@RestController
@Api(tags = "管理员模块")
@RequestMapping("/ums/admin/umsAdmin")
public class UmsAdminController {

    @Resource
    IUmsAdminService iUmsAdminService;
    @Resource
    UmsAdminServiceImpl umsAdminService;

    @Resource
    ConstClass constClass;

    @ApiOperation("nacso案例,配置中心")
    @GetMapping("/nacosconfig")
    public RespMessUtil<String> nacosConfig(){
        return RespMessUtil.build(CodeStatusEnum.SUCCESS,constClass.getTipUploadAvatarFail());
    }

    @ApiOperation("后台_用户登录功能")
    @PostMapping("/login")
    public RespMessUtil<UmsAdminVo> login(
            @RequestBody UserInfo userInfo
    ) {
        return iUmsAdminService.login(userInfo);
    }

    @ApiOperation("个人中心修改个人信息功能")
    @PostMapping("/modifyUser")
    public RespMessUtil<Boolean> modifyUser(
            @RequestBody UmsAdmin user
    ) {
        return iUmsAdminService.modifyUser(user);
    }


    @ApiOperation("头像功能处理")
    @PostMapping("/uploadAvatar/{id}")
    public RespMessUtil<JSONObject> uploadAvatar(
            @PathVariable("id") Long id,
            @ApiParam("avatarFile") @RequestParam("avatarFile") MultipartFile avatarFile
    ) {
        return iUmsAdminService.uploadAvatar(id, avatarFile);
    }

    /**
     * 用户注册
     *
     * @param u
     * @param RoleIds
     * @return
     */
    @ApiOperation("后台_用户注册功能")
    @PostMapping("/register")
    public RespMessUtil<Integer> register(@RequestBody AdminInfo u, @RequestParam(value = "RoleIds", required = false) List<Long> RoleIds) {
        return umsAdminService.register(u, RoleIds);

    }


    @ApiOperation("后台_用户修改密码功能")
    @PutMapping("/modifyAdminPass")
    public RespMessUtil<Integer> modifyAdminPass(@RequestParam("id") int id, @RequestParam("password") String password) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, iUmsAdminService.modifyAdminPass(id, password));
    }

    @ApiOperation("后台_用户分页查询功能")
    @GetMapping("/showAdmin")
    public RespMessUtil<IPage> showAdmin(
            @ApiParam(value = "用户", name = "username") @RequestParam(value = "username", required = false) String username,
            @ApiParam(value = "状态", name = "status") @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "当前页码", name = "pageSize", required = true) @RequestParam(value = "pageSize", defaultValue = "1") int pageSize,
            @ApiParam(value = "显示条数", name = "pageLimit", required = true) @RequestParam(value = "pageLimit", defaultValue = "10") int pageLimit) {
        return iUmsAdminService.showAdminWithPage(username, status, pageSize, pageLimit);
    }

    @PutMapping("/updateRole")
    @ApiOperation("管理员更改信息")
    public RespMessUtil<Integer> updateRole(@RequestBody AdminInfo ur, @RequestParam(value = "roleIds", required = false) List<Long> roleIds) {

        return umsAdminService.updateById(ur, roleIds);

    }


    @ApiOperation("后台_用户删除功能")
    @DeleteMapping("/deleteAdmin/{id}")
    public RespMessUtil<Integer> deleteAdmin(
            @ApiParam(name = "id", value = "主键", required = true) @PathVariable("id") int id
    ) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, iUmsAdminService.deleteAdmin(id));
    }

    @ApiOperation("后台_用户批量删除功能")
    @DeleteMapping("/deleteAllAdmin")
    public RespMessUtil<Integer> deleteAllAdmin(
            @ApiParam(name = "ids", value = "主键", required = true) @RequestParam("ids") List<Integer> ids
    ) {
        int i = iUmsAdminService.deleteAllAdmin(ids);
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, i);
    }

    @ApiOperation("切换状态")
    @PutMapping("/switchStatus/{id}")
    public RespMessUtil<Boolean> switchStatus(
            @ApiParam(name = "id", value = "主键", required = true) @PathVariable("id") int id
    ) {
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, iUmsAdminService.switchStatus(id));
    }

}
